<template>
  <div>
    <div class="content">
      <a-form
        @submit="handleSubmit"
        style="width: 70%; margin: 0px auto"
        :form="form"
      >
        <div>
          <agent-rate
            :MerRateList="MerRateList"
            ref="getMerRateData"
          ></agent-rate>
        </div>

        <a-form-item v-bind="formItemLayout" label="分润比例">
          <div style="display: flex">
            <div>
              <a-input
                v-decorator="[
                  'profitPercent',
                  {
                    rules: [{ required: true, message: '请输入分润比例' }],
                    initialValue: '100',
                  },
                ]"
                placeholder="请输入分润比例"
                @change="agentCodeNameChange"
              />
            </div>
            <div>%</div>
          </div>
        </a-form-item>
        <a-form-item v-bind="formItemLayout" label="分润结算方式">
          <a-radio-group
            :disabled="disabled"
            v-decorator="[
              'profitSettleType',
              {
                rules: [
                  {
                    required: true,
                    message: '请选择分润结算方式',
                  },
                ],
                initialValue: 'self',
              },
            ]"
            @change="shareTypeChange"
          >
            <a-radio value="self">结算本级</a-radio>
            <a-radio value="higher">结算上级</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item style="width: 80%; margin: 0px auto">
          <AddedFunctionConfig
            :FunctionLists="FunctionLists"
            v-if="FunctionLists.length"
            ref="childTool"
            @getChildFun="getChildFun"
          />
        </a-form-item>
        <a-row>
          <a-col :xs="{ span: 24 }" :sm="{ span: 12, offset: 7 }">
            <div style="margin-top: 30px">
              <a-button
                class="submitButton"
                :loading="loading"
                type="primary"
                html-type="submit"
                >下一步</a-button
              >
            </div>
          </a-col>
        </a-row>
      </a-form>
    </div>
  </div>
</template>
<script>
import { message } from "ant-design-vue";
import api from "@/common/api";
import { error } from "util";
import { fail } from "assert";
import { locale } from "moment";
import AgentRate from "../../common/AgentRate";
import AddedFunctionConfig from "@/components/AddedFunction/AddedFunctionConfig";

export default {
  components: {
    AgentRate,
    AddedFunctionConfig,
  },
  data() {
    return {
      FunctionLists: [], //  增值功能
  
      feeTemplateUpdateRequests: [],
      addedValues: [], // 增值功能提交参数
      MerRateList: null,
      selectOptions: [],
      radiolist: [],
      ratelist: [],
      product: "",
      defaultValue: "请选择",
      modify: "",
      loading: false,
      useApi: true,
      docking: true,
      disabled: false,
      Resubmit: false,
      values: null,
      radiovalue: 1,
      radioid: 1,
      radioStyle: {
        display: "block",
        height: "30px",
        lineHeight: "30px",
      },
      posTransShow: false,
      initialValue: [],
      checkedValues: [],
      posTransArray: [],
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15, offset: 1 },
          md: { span: 12, offset: 1 },
        },
      },
      userdata: {},
      posConfigs: {},
      form: this.$form.createForm(this),
      memberNo: null,
      queryid: null,
      agentid: null,
      bankCardid: null,
      smallid: null,
      legalIdentityid: null,
      isPrivate: Boolean,
    };
  },
  filters: {
    transNamefilter(value) {
      return value.substring(4);
    },
  },
  created() {},
  mounted() {
    console.log(this.$route.params.queryid);
    this.queryid = this.$route.params.queryid;
    // this.queryid = "1585114759007018";
    this.getProduct();
  },
  methods: {
    handleSubmit(e) {
      e.preventDefault();
      // 判断add 增值逻辑功能

      this.form.validateFields((err, values) => {
        if (!err) {
          // console.log("values", values);

          this.userdata = {};
          if (this.merchantType == "small") {
            let orguserdata = {
              agentInfoUpdateRequest: {
                memberNo: this.memberNo,
                merchantType: this.merchantType,
                id: this.agentid,
                profitPercent: values.profitPercent,
                profitSettleType: values.profitSettleType,
              },
              smallLogIdentity: {
                id: this.legalIdentityid,
              },
              logBankCard: {
                id: this.bankCardid,
              },
            };
            this.userdata = orguserdata;
          } else {
            let meruserdata = {
              agentInfoUpdateRequest: {
                memberNo: this.memberNo,
                merchantType: this.merchantType,
                id: this.agentid,

                profitPercent: values.profitPercent,
                profitSettleType: values.profitSettleType,
              },
              legalLogIdentity: {
                id: this.legalIdentityid,
              },
              logBankCard: {
                id: this.bankCardid,
                isPrivate: this.isPrivate,
              },
            };
            this.userdata = meruserdata;
          }
          let feeTemplateUpdateRequests = [];
          if (this.Resubmit == true) {
            this.$refs.getMerRateData.rateConversion();

            if (this.$refs.getMerRateData.feeTemplateList.length == 0) {
              console.log("数组为0");
              return false;
            }
            // console.log("通过",this.$refs.getMerRateData.feeTemplateList)
            feeTemplateUpdateRequests =
              this.$refs.getMerRateData.feeTemplateList;
          } else {
            this.$refs.getMerRateData.agentRateConversion();

            if (this.$refs.getMerRateData.feeTemplateList.length == 0) {
              console.log("数组为0");
              return false;
            }
            // console.log("通过",this.$refs.getMerRateData.feeTemplateList)
            feeTemplateUpdateRequests =
              this.$refs.getMerRateData.feeTemplateList;
          }

          this.feeTemplateUpdateRequests = feeTemplateUpdateRequests;
          if (this.FunctionLists.length) {
            this.$refs.childTool.PopAddFunction();
          } else {
            this.getAgentUpdateInfo();
          }
        }
      });
    },
    getChildFun(payload) {
      this.addedValues = payload.list;
      this.getAgentUpdateInfo();
    },

    jumpTopage() {
      this.$router.replace({
        path: "/Partners/step4",
        query: { success: "true", memberNo: this.memberNo },
      });
    },

    getAgentUpdateInfo() {
      console.log("getAgentUpdateInfo");
      this.loading = true;
      this.$axios
        .post("/agent/logAgent/update/fee", {
          agentInfo: this.userdata,
          fee: this.feeTemplateUpdateRequests,
          list: this.addedValues,
        })
        .then(
          (res) => {
            // console.log(res);
            if (res.data.code == "OK") {
              this.loading = false;
              this.jumpTopage();
    
            } else {
              this.loading = false;
              this.$message.warning(res.data.message);
            }
          },
          (err) => {
            this.loading = false;
          }
        );
    },

    getfee() {
      this.$axios.get("/agent/logAgent/fetch/fee/" + this.memberNo).then(
        (res) => {
          if (res.data.code == "OK") {
            this.Resubmit = true;
            this.form.setFieldsValue({profitPercent:res.data.data.profitPercent})
            if (res.data.data.settleType == "higher") {
              this.disabled = true;
              this.form.setFieldsValue({ profitSettleType: "higher" });
            } else {
              this.disabled = false;
            }

            this.MerRateList = res.data.data.fee;

            // console.log("this.ratelist", this.ratelist);
          } else {
            this.Resubmit = false;
            this.getratelist();
            this.getsettleType();
          }
        },
        (err) => {}
      );
    },
    getsettleType() {
      this.$axios.get("/agent/logAgent/settleType").then((res) => {
        // console.log("res", res);
        if (res.data.data.settleType == "higher") {
          this.disabled = true;
          this.form.setFieldsValue({ profitSettleType: "higher" });
        } else {
          this.disabled = false;
        }
      });
    },
    getProduct() {
      this.$axios.get("/agent/logAgent/fetch/" + this.queryid).then((res) => {
        if (res.data.code == "OK") {
          this.modify = res.data.data;
          this.merchantType = this.modify.agent.merchantType;
          this.memberNo = this.modify.agent.memberNo;

          // add 增值功能
          this.getValuedAddFunctionTools();
          this.isPrivate = this.modify.bankCard.isPrivate;

          this.agentid = Number(this.modify.agent.id);
          this.bankCardid = Number(this.modify.bankCard.id);
          if (this.modify.legalIdentity) {
            this.legalIdentityid = Number(this.modify.legalIdentity.id);
          } else {
            this.legalIdentityid = Number(this.modify.smallIdentity.id);
          }
          // this.smallid = Number(this.modify.smallIdentity.id);
          if (this.memberNo) {
            // console.log(this.memberNo)
            this.getfee();
          }
        } else {
          this.$message.error(res.data.message);
        }
      });
    },

    getValuedAddFunctionTools() {
      var data = {
        memberNo: this.memberNo,
      };
      this.$axios.post("/agent/added/value/defaultConfig", data).then((res) => {
        if (res.data.code == "OK") {
          this.FunctionLists = res.data.list || [];
        } else {
          this.FunctionLists = [];
          this.$message.error(res.data.message);
        }
      });
    },

    shareTypeChange(e) {
      console.log("value", e.target.value);
    },
    agentCodeNameChange(e) {},

    getratelist() {
      this.$axios.get("/agent/logAgent/business/query").then((res) => {
        console.log("ratelist", res);
        if (res.data.code == "OK") {
          this.MerRateList = res.data.data;
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
/deep/.noExpand {
  .ant-table-row-expand-icon {
    display: none;
  }
}
a {
  text-decoration: none;
}
.content {
  /* text-align: center */
  margin: 0 auto;
}

.card {
  margin-top: 50px;
}

.card .title {
  text-align: center;
  padding: 20px;
  font-size: 18px;
}

.btn-next {
  margin-top: 20px;
}

.btn-style {
  background: #ccc;
  border: 1px solid #ccc;
  color: #eee;
}

.ant-checkbox-group .ant-checkbox-group-item {
  width: 100%;
}

.w700 {
  width: 800px;
  margin-top: 50px;
  /* margin-bottom: 14px; */
}

.title-sty {
  padding-right: 30px !important;
}

.submitButton {
  width: 8vw;
}
</style>